package com.common

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

// 消息窗口
class MessageBox {
    private var mCancelButtonText = ""                  // "取消"按钮上显示的文字
    private var mOnCancel: () -> Unit = {}              // 用户单击"取消"按钮事件
    private var mOKButtonText = ""                      // "确定"按钮上显示的文字
    private var mOnOK: () -> Unit = {}                  // 用户单击"确定"按钮事件
    private lateinit var mShow: MutableState<Boolean>   // 是否显示
    private var mTipText = ""                           // 提示文字

    /**
     * 界面布局。
     */
    @Composable
    fun Greeting() {
        mShow = remember {
            mutableStateOf(false)
        }
        if (mShow.value) {
            AlertDialog(
                onDismissRequest = { mShow.value = false },
                text = {
                    Text(mTipText)
                },
                confirmButton = if (mOKButtonText.isEmpty()) { {} } else {{
                    TextButton({
                        this.mOnOK()

                        // 关闭窗口
                        mShow.value = false
                    }) {
                        Text(mOKButtonText)
                    }
                }},
                dismissButton = if (mCancelButtonText.isEmpty()) { {} } else {{
                    TextButton({
                        this.mOnCancel()

                        // 关闭窗口
                        mShow.value = false
                    }) {
                        Text(mCancelButtonText)
                    }
                }}
            )
        }
    }

    /**
     * 显示。
     * @param cancelButtonText  "取消"按钮上显示的文字
     * @param onCancel          用户单击"取消"按钮事件
     * @param okButtonText      "确定"按钮上显示的文字
     * @param onOK              用户单击"确定"按钮事件
     * @param tipText           提示文字
     */
    fun show(cancelButtonText: String = "", onCancel: () -> Unit = {}, okButtonText: String = "", onOK: () -> Unit = {}, tipText: String = "") {
        mCancelButtonText = cancelButtonText
        mOnCancel = onCancel
        mOKButtonText = okButtonText
        mOnOK = onOK
        mTipText = tipText
        mShow.value = true
    }
}
